<?php

namespace App\Http\Controllers\Network;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Pay\Pay;
use App\Http\Requests\Network\Settlement\NetworkCharge;
use App\Http\Requests\Network\Settlement\ShowRequest;
use App\Http\Requests\Network\Settlement\QueryResource;
use App\Repositories\SettlementRepository;
use App\Repositories\UserLogRepository;
use Illuminate\Support\Facades\Auth;

class SettlementController extends Controller
{
    protected $settlementRepository;

    /**
     * HomesController constructor.
     * @param SettlementRepository $settlementRepository
     */
    public function __construct(
        SettlementRepository $settlementRepository
    )
    {
        $this->settlementRepository = $settlementRepository;
    }

    /**
     * 获取网点账单列表
     *
     * Date: 2018/06/02
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $date = $request->get('date', false);
        $limit = $request->get('paginate', 15);
        $admins = Auth::guard('network')->user();
        $network_id = $admins->network_id;
        if ($date === null) {
            $date = false;
        }
        $homes = $this->settlementRepository->getNetworkBills($network_id, $date, $limit);
        return success($homes);
    }

    /**
     * 获取网点账单列表详情
     *
     * Date: 2018/06/02
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request)
    {
        $date = $request->get('date', false);
        $service_id = $request->get('service_id', false);
        $limit = $request->get('paginate', 15);
        if ($date === null) {
            $date = false;
        }
        if ($service_id === null) {
            $service_id = false;
        }
        $admins = Auth::guard('network')->user();
        $network_id = $admins->network_id;
        $homes = $this->settlementRepository->getNetworkBillDetails($network_id, $service_id, $date, $limit);
        return success($homes);
    }

    /**
     * 充值
     *
     * Date: 2018/05/18
     * @author Tenny
     * @param NetworkCharge $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function charge(NetworkCharge $request)
    {
        $pay = new Pay();
        $id = date('YmdHis');
        $amount = $request->amount;
        $res = $pay->newPay($id, $amount);
        return respond([], '结算成功！', 200);
    }
}
